package com.crayfish.quduo.presenter;

import com.crayfish.quduo.App;
import com.crayfish.quduo.base.RxPresenter;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.contract.LoginContract;
import com.crayfish.quduo.utils.BmobManager;
import com.crayfish.quduo.utils.LogUtils;
import com.crayfish.quduo.utils.SharedPreferencesUtil;
import com.crayfish.quduo.utils.ToastUtil;
import com.mob.tools.utils.Data;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by crayfish on 2017/3/2.
 */
public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presernter {

    @Override
    public void login() {
        UserBean userBean = SharedPreferencesUtil.getUser(App.getInstance().getApplicationContext());
        UserBean user = new UserBean();
        user.setUsername(userBean.getUsername());
        user.setPassword(Data.MD5(userBean.getUserId()));
        user.login(new SaveListener<UserBean>() {
            @Override
            public void done(UserBean userBean, BmobException e) {
                if(e == null) {
                    mView.successLogin(userBean);
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void register() {
        UserBean userBean = SharedPreferencesUtil.getUser(App.getInstance().getApplicationContext());
        UserBean bean = new UserBean();
        bean.setUsername(userBean.getUsername());
        bean.setPassword(Data.MD5(userBean.getUserId()));
        bean.setPortrait(userBean.getPortrait());
        bean.signUp(new SaveListener<UserBean>() {
            @Override
            public void done(UserBean userBean, BmobException e) {
                if(e == null){
                    if(e == null) {
                        mView.successRegister();
                    }
                }else {
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }

    @Override
    public void searchUser(String username) {
        BmobQuery<UserBean> query = new BmobQuery<UserBean>();
        query.addWhereEqualTo("username",username);
        query.findObjects(new FindListener<UserBean>() {
            @Override
            public void done(List<UserBean> list, BmobException e) {
                if(e == null){
                    if(list.size() > 0){//进行登录
                        mView.loginOrRegister(true);
                    }else{//进行注册
                        mView.loginOrRegister(false);
                    }
                }else{
                    mView.showError(e.getMessage());
                    LogUtils.d(e.getMessage());
                }
            }
        });
    }
}
